package edu.jmu.flowmeter.controller;

import edu.jmu.flowmeter.domain.User;
import edu.jmu.flowmeter.dto.AbstractBaseResult;
import edu.jmu.flowmeter.service.RedisService;
import edu.jmu.flowmeter.service.UserService;
import edu.jmu.flowmeter.validator.BeanValidator;
import edu.jmu.flowmeter.web.AbstractBaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * @author Wen
 */

@RestController
@RequestMapping(value = "user")
public class UserController extends AbstractBaseController<User> {

    @Autowired
    UserService userService;

    @Autowired
    private RedisService redisService;


    @PostMapping(value = "reg")
    public AbstractBaseResult regUser(User user) {

        //校验是否能为实体类
        String message = BeanValidator.validator(user);

        if (StringUtils.isNotBlank(message)) {
            return error(HttpStatus.UNAUTHORIZED.value(), message, null);

        }

        //校验用户名是否重复
        if (!userService.unique("username", user.getUsername())) {
            return error(HttpStatus.UNAUTHORIZED.value(), "用户名重复请重试！", null);
        }

        //密码MD5加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes(StandardCharsets.UTF_8)));
        User user1 = userService.save(user);
        if (user1!=null){
            response.setStatus(HttpStatus.CREATED.value());
            return success(user);
        }
        return error(HttpStatus.UNAUTHORIZED.value(), "注册失败，请重试！", null);
    }

    @PostMapping(value = "login")
    public AbstractBaseResult login(User user){
        //校验是否能为实体类
        System.out.println(1);
        String message = BeanValidator.validator(user);
        if (StringUtils.isNotBlank(message)) {
            return error(HttpStatus.UNAUTHORIZED.value(), message, null);

        }
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes(StandardCharsets.UTF_8)));

        if (!userService.unique("username",user.getUsername())){
            if (!userService.unique("password",user.getPassword())){
                response.setStatus(HttpStatus.OK.value());
                String usernameMD5 = DigestUtils.md5DigestAsHex(user.getUsername().getBytes(StandardCharsets.UTF_8));
                String tokenRandom = UUID.randomUUID().toString();
                String token = usernameMD5+"."+tokenRandom;
                //设置cookie
                Cookie cookie=new Cookie("token",token);
                cookie.setPath("/");
                //cookie.setDomain("localhost");
                response.addCookie(cookie);
                redisService.set(token,user.getUsername());
                return success(user);
            }else {
                return error(HttpStatus.UNAUTHORIZED.value(), "密码错误请重试！", null);
            }
        }

        return error(HttpStatus.UNAUTHORIZED.value(), "用户名错误，请重试！", null);

    }


}
